package com.paishen.peiniwan.frame;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.paishen.peiniwan.R;

import java.util.ArrayList;
import java.util.List;


public abstract class RecyclerAdapter extends RecyclerView.Adapter {
    protected static class Element {
        public static final int TYPE_ITEM = 1;
        public static final int TYPE_LOAD = 2;
        public int type;
        public Object element;
    }

    public static int FETCH_COUNT = 10;
    protected final List<Element> elements = new ArrayList<>();
    protected final Element eLoad;

    public RecyclerAdapter() {
        eLoad = new Element();
        eLoad.type = Element.TYPE_LOAD;
        eLoad.element = true;
    }

    public boolean set(List<? extends Object> dataList, boolean fetchOver) {
        if (dataList != null) {
            elements.clear();
            for (Object o : dataList) {
                Element eItem = new Element();
                eItem.type = Element.TYPE_ITEM;
                eItem.element = o;
                elements.add(eItem);
            }
            if (!fetchOver) {
                elements.add(eLoad);
            }
            notifyDataSetChanged();
            return true;
        }
        return false;
    }

    public boolean append(List<? extends Object> dataList, boolean fetchOver) {
        if (dataList != null) {
            int loadIdx = elements.indexOf(eLoad);
            if (loadIdx >= 0) {
                elements.remove(loadIdx);
                notifyItemRemoved(loadIdx);
            }

            for (Object o : dataList) {
                Element eItem = new Element();
                eItem.type = Element.TYPE_ITEM;
                eItem.element = o;
                elements.add(eItem);
            }
            if (!fetchOver) {
                elements.add(eLoad);
            }
            notifyItemRangeInserted(loadIdx, dataList.size() + 1);
            return true;
        }
        return false;
    }

    public final boolean refreshLoadState(boolean shown) {
        if (elements.size() > 0) {
            eLoad.element = shown;
            int loadIdx = elements.indexOf(eLoad);
            if (loadIdx >= 0) {
                notifyItemChanged(loadIdx);
                return true;
            }
        }
        return false;
    }

    public class ViewHolderLoad extends RecyclerView.ViewHolder {
        public ViewHolderLoad(View view) {
            super(view);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case Element.TYPE_LOAD:
                View viewLoad = LayoutInflater.from(parent.getContext()).inflate(R.layout.frame_recycler_load, parent, false);
                return new ViewHolderLoad(viewLoad);
        }
        throw new IllegalArgumentException("RecyclerAdapter(" + getClass().getSimpleName() + ") unhandle viewType :" + viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Element e = elements.get(position);
        switch (e.type) {
            case Element.TYPE_LOAD:
                ViewHolderLoad vhLoad = (ViewHolderLoad) holder;
                vhLoad.itemView.setVisibility(((boolean) e.element) ? View.VISIBLE : View.GONE);
                break;
        }
    }

    @Override
    public final int getItemCount() {
        return elements.size();
    }

    @Override
    public final int getItemViewType(int position) {
        return elements.get(position).type;
    }
}
